package org.mango.handler;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;

/**
 * Created by meigang on 2019/8/8.
 */
public class EchoServerHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        //ctx.write(msg); // (1)
        //ctx.flush(); // (2)
        //收到的数据，原封不动的返回
        ByteBuf in = (ByteBuf) msg;
        String inStr = in.toString(CharsetUtil.US_ASCII);
        if("exit".equals(inStr.replace("\r\n",""))){//如果是输入的exit回车，就断开当前连接
            System.out.println("断开连接");
            ctx.close();
        }else{
            System.out.print("回写:"+inStr);
            ctx.writeAndFlush(msg);
        }
    }
}

